public class Test1 {
    //leetcode 峰与谷
    //https://leetcode.cn/problems/peaks-and-valleys-lcci/description/?envType=problem-list-v2&envId=greedy
    public void wiggleSort(int[] nums) {
        //贪心
        //整个数组按照峰-谷-峰的顺序排列，那么当i%2==0也就是该位置为峰，
        //如果nums[i]<nums[i-1]那么就交换i和i-1，谷也是一样
        for (int i = 1; i < nums.length; i++) {
            if (i % 2 == 0 && nums[i] < nums[i-1]) swap(nums,i-1,i);
            else if (i % 2 == 1 && nums[i] > nums[i-1]) swap(nums,i-1,i);
        }
    }

    private void swap(int[] nums,int x,int y) {
        int tmp = nums[x];
        nums[x] = nums[y];
        nums[y] = tmp;
    }
}
